<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微信登录回调</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 400px;
            width: 100%;
        }
        .logo {
            width: 60px;
            height: 60px;
            background: #07C160;
            border-radius: 12px;
            margin: 0 auto 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 24px;
        }
        p {
            color: #666;
            margin-bottom: 20px;
            line-height: 1.5;
        }
        .status {
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            font-weight: 500;
        }
        .success {
            background: #e8f5e8;
            color: #2d5a2d;
            border: 1px solid #4caf50;
        }
        .error {
            background: #ffeaea;
            color: #d32f2f;
            border: 1px solid #f44336;
        }
        .loading {
            background: #e3f2fd;
            color: #1976d2;
            border: 1px solid #2196f3;
        }
        .btn {
            background: #07C160;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
        }
        .btn:hover {
            background: #06ad56;
        }
        .btn:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .code {
            background: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            margin: 10px 0;
            word-break: break-all;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">W</div>
        <h1>微信登录回调</h1>
        <p>正在处理微信授权回调...</p>
        
        <div id="status" class="status loading">
            正在验证授权码...
        </div>
        
        <div id="details" style="display: none;">
            <h3>回调详情</h3>
            <div id="codeInfo"></div>
            <div id="resultInfo"></div>
        </div>
        
        <button id="closeBtn" class="btn" style="display: none;" onclick="closeWindow()">
            关闭窗口
        </button>
    </div>

    <script>
        // 获取URL参数
        function getUrlParams() {
            const params = new URLSearchParams(window.location.search);
            return {
                code: params.get('code'),
                state: params.get('state'),
                error: params.get('error'),
                error_description: params.get('error_description')
            };
        }

        // 发送授权码到后端
        async function sendCodeToBackend(code) {
            try {
                const response = await fetch('/users/wechat/callback', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ code })
                });

                const result = await response.json();
                return result;
            } catch (error) {
                console.error('发送授权码失败:', error);
                return { success: false, message: '网络错误' };
            }
        }

        // 显示详情
        function showDetails(params, result) {
            const detailsDiv = document.getElementById('details');
            const codeInfo = document.getElementById('codeInfo');
            const resultInfo = document.getElementById('resultInfo');
            
            codeInfo.innerHTML = `
                <p><strong>授权码:</strong></p>
                <div class="code">${params.code || '无'}</div>
                <p><strong>状态参数:</strong></p>
                <div class="code">${params.state || '无'}</div>
            `;
            
            if (result) {
                resultInfo.innerHTML = `
                    <p><strong>处理结果:</strong></p>
                    <div class="code">${JSON.stringify(result, null, 2)}</div>
                `;
            }
            
            detailsDiv.style.display = 'block';
        }

        // 关闭窗口
        function closeWindow() {
            window.close();
        }

        // 主处理逻辑
        async function handleCallback() {
            const params = getUrlParams();
            const statusDiv = document.getElementById('status');
            const closeBtn = document.getElementById('closeBtn');
            
            // 显示回调参数
            console.log('微信回调参数:', params);
            
            // 检查是否有错误
            if (params.error) {
                statusDiv.className = 'status error';
                statusDiv.textContent = `授权失败: ${params.error_description || params.error}`;
                closeBtn.style.display = 'inline-block';
                showDetails(params);
                return;
            }
            
            // 检查是否有授权码
            if (!params.code) {
                statusDiv.className = 'status error';
                statusDiv.textContent = '未获取到授权码';
                closeBtn.style.display = 'inline-block';
                showDetails(params);
                return;
            }
            
            // 发送授权码到后端
            statusDiv.textContent = '正在处理登录...';
            const result = await sendCodeToBackend(params.code);
            
            if (result.success) {
                statusDiv.className = 'status success';
                statusDiv.textContent = '登录成功！正在跳转...';
                
                // 向父窗口发送成功消息
                if (window.opener) {
                    window.opener.postMessage({
                        type: 'wechat_login_success',
                        code: params.code,
                        result: result
                    }, '*');
                }
                
                // 延迟关闭窗口
                setTimeout(() => {
                    window.close();
                }, 2000);
            } else {
                statusDiv.className = 'status error';
                statusDiv.textContent = `登录失败: ${result.message}`;
                closeBtn.style.display = 'inline-block';
            }
            
            showDetails(params, result);
        }

        // 页面加载完成后处理回调
        document.addEventListener('DOMContentLoaded', handleCallback);
    </script>
</body>
</html> 